<template>
  <div>
    <h1>门禁卡管理</h1>
    <el-form ref="form" :model="selectForm" style="display: flex;width: 100%;">
      <el-form-item label="人名" style="display: flex;width: 200px;">
        <el-input
          size="mini"
          v-model="selectForm.populationName"
          style="width: 100px;"
        ></el-input>
      </el-form-item>
      <el-form-item label="小区名称" style="display: flex;width: 200px;">
        <el-input
          size="mini"
          v-model="selectForm.residenceName"
          style="width: 100px;"
        ></el-input>
      </el-form-item>
      <el-form-item label="房屋号" style="display: flex;width: 200px;">
        <el-input
          size="mini"
          v-model="selectForm.roomNumber"
          style="width: 100px;"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="mini" type="primary" @click="getEntranceGuardCardList">查询</el-button>
        <el-button size="mini">取消</el-button>
        <el-button size="mini"
                   style="margin-left: 200px" type="primary"
                   @click="dialogFormVisibleType = true;
                   dialogFormVisible = true;
                   title = '新建门禁卡';
                   form = {}"
        >新建
        </el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="entranceGuardCardList" style="width: 100%"
    >
      <el-table-column label="卡号">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.cardNumber }}</span>
        </template>
      </el-table-column>
      <el-table-column label="所属人名">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.populationName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建日期">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.creationDate }}</span>
        </template>
      </el-table-column>
      <el-table-column label="所属小区名称">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.residenceName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="所属房屋号">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.roomNumber }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini"
                     @click="handleEdit(scope.row.id);
                     dialogFormVisible = true;
                     dialogFormVisibleType = false;
                     title = '编辑门禁卡';"
          >编辑
          </el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div>
      <el-dialog :title="title" :visible.sync="dialogFormVisible">
        <el-form :model="form">
          <el-form-item label="卡号">
            <el-input v-model="form.cardNumber" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false;form = {}">取 消</el-button>
          <el-button type="primary" v-show="dialogFormVisibleType"
                     @click="handleInsert()">新 建</el-button>
          <el-button type="primary" v-show="!dialogFormVisibleType"
                     @click="handleUpd()">修 改</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>

import {
  addEntranceGuardCard,
  deleteEntranceGuardCard,
  editEntranceGuardCard,
  entranceGuardCardInfo,
  entranceGuardCardList
} from '@/api/manage/card'

export default {
  data() {
    return {
      entranceGuardCardList: [],
      title: '',
      dialogFormVisible: false,
      form: {
        cardNumber: '',
        populationId: null
      },
      selectForm: {
        populationName: '',
        residenceName: '',
        roomNumber: '',
        pageNum: 1,
        pageSize: 10
      },
      dialogFormVisibleType: false
    }
  },
  methods: {
    getEntranceGuardCardList() {
      entranceGuardCardList(this.selectForm).then(response => {
        this.entranceGuardCardList = response.data.list
      })
    },
    handleEdit(ver) {
      entranceGuardCardInfo(ver).then(response => {
        this.form = response.data
      })
    },
    handleDelete(ver) {
      deleteEntranceGuardCard(ver).then(response => {
        this.$message.success('删除成功')
        this.getEntranceGuardCardList()
      })
    },
    handleInsert() {
      addEntranceGuardCard(this.form).then(response => {
        this.$message.success('新建成功')
        this.getEntranceGuardCardList()
      })
      this.dialogFormVisible = false;
      this.form = {}
    },
    handleUpd() {
      editEntranceGuardCard(this.form).then(response => {
        this.$message.success('修改成功')
        this.getEntranceGuardCardList()
      })
      this.dialogFormVisible = false;
      this.form = {}
    }
  },
  created() {
    this.getEntranceGuardCardList()
  }
}
</script>

<style scoped lang="scss">
</style>
